home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-13 | 9.9 KB | 493 lines | [TEXT/CWIE] |
- /*
- File: PickerCommonUtils.c
- Copyright: © 1996-1997 by Apple Computer, Inc., all rights reserved.
- */
-
-
- //===============================================================================
- //
- // PickerCommonUtils.c
- //
- //===============================================================================
-
-
- #include <ColorPicker.h>
- #include <Resources.h>
- #include <TextUtils.h>
- #include <ToolUtils.h>
- #include "PickerCommon.h"
-
-
- #ifdef COMPONENT_SIGNATURE
- #undef COMPONENT_SIGNATURE
- #endif
- #define COMPONENT_SIGNATURE 'pcmn'
-
-
- // Reserve size for SmartNewHandle() and SmartNewPtr() .
- #define kMemReserve 32767
-
- // Return values for ChooseHeap().
- enum
- {
- noHeap = 0,
- currentHeap,
- systemHeap,
- tempHeap
- };
-
-
- static long ChooseHeap (Size, Size);
- static OSErr GetZoneSizes (Size *, Size *, Size *);
- static long LargestHeap (Size, Size, Size);
- static void SetMem (void *, Size, UInt8);
-
-
- //===================================================================== Functions
- //--------------------------------------------------------------------- PasStringCopy
-
- void PasStringCopy (StringPtr p1, StringPtr p2)
- {
- register short stringLength;
-
- stringLength = *p2++ = *p1++;
- while (--stringLength >= 0)
- *p2++ = *p1++;
- }
-
- //--------------------------------------------------------------------- HappiNewHandle
-
- Handle HappiNewHandle (Size byteCount, OSErr *resultErr)
- {
- Handle theHandle;
- short whichHeap;
- OSErr theErr;
-
- whichHeap = ChooseHeap(byteCount, kMemReserve);
-
- switch (whichHeap)
- {
- case noHeap:
- theHandle = 0L;
- theErr = memFullErr;
- break;
-
- case currentHeap:
- theHandle = NewHandle(byteCount);
- theErr = MemError();
- break;
-
- case systemHeap:
- theHandle = NewHandleSys(byteCount);
- theErr = MemError();
- break;
-
- case tempHeap:
- theHandle = TempNewHandle(byteCount, &theErr);
- break;
-
- // Error in ChooseHeap()
- default:
- theHandle = 0L;
- theErr = paramErr;
- break;
- }
-
- if (!theErr && !theHandle)
- theErr = memFullErr;
-
- if (theErr && theHandle)
- theHandle = 0L;
-
- if (resultErr)
- *resultErr = theErr;
-
- check((theErr == noErr) && (theHandle));
-
- return theHandle;
- }
-
- //--------------------------------------------------------------------- HappiNewHandleClear
-
- Handle HappiNewHandleClear (Size byteCount, OSErr *resultErr)
- {
- Handle theHandle;
- short whichHeap;
- OSErr theErr;
-
- whichHeap = ChooseHeap(byteCount, kMemReserve);
-
- switch (whichHeap)
- {
- case noHeap:
- theHandle = 0L;
- theErr = memFullErr;
- break;
-
- case currentHeap:
- theHandle = NewHandleClear(byteCount);
- theErr = MemError();
- break;
-
- case systemHeap:
- theHandle = NewHandleSysClear(byteCount);
- theErr = MemError();
- break;
-
- case tempHeap:
- theHandle = TempNewHandle(byteCount, &theErr);
- if (theHandle && !theErr)
- SetMem( *theHandle, byteCount, 0x00);
- break;
-
- default: /* Error in ChooseHeap(…) */
- theHandle = 0L;
- theErr = paramErr;
- break;
- }
-
- if (!theErr && !theHandle)
- theErr = memFullErr;
-
- if (theErr && theHandle )
- theHandle = 0L;
-
- if (resultErr)
- *resultErr = theErr;
-
- check((theErr == noErr) && (theHandle));
-
- return theHandle;
- }
-
- //--------------------------------------------------------------------- HappiNewPointer
-
- Ptr HappiNewPointer (Size byteCount, OSErr *resultErr)
- {
- Ptr thePointer;
- SInt16 whichHeap;
- OSErr theErr;
-
- whichHeap = ChooseHeap(byteCount, kMemReserve);
-
- switch (whichHeap)
- {
- case noHeap:
- thePointer = 0L;
- theErr = memFullErr;
- break;
-
- case currentHeap:
- thePointer = NewPtr(byteCount);
- theErr = MemError();
- break;
-
- // System heap is growable into temp zone.
- case systemHeap:
- case tempHeap:
- thePointer = NewPtrSys(byteCount);
- theErr = MemError();
- break;
-
- // Error in ChooseHeap().
- default:
- thePointer = 0L;
- theErr = paramErr;
- break;
- }
-
- if (!theErr && !thePointer)
- theErr = memFullErr;
-
- if (theErr && thePointer)
- thePointer = 0L;
-
- if (resultErr)
- *resultErr = theErr;
-
- check((theErr == noErr) && (thePointer));
-
- return thePointer;
- }
-
- //--------------------------------------------------------------------- HappiNewPointerClear
-
- Ptr HappiNewPointerClear (Size byteCount, OSErr *resultErr)
- {
- Ptr thePointer;
- SInt16 whichHeap;
- OSErr theErr;
-
- whichHeap = ChooseHeap(byteCount, kMemReserve);
-
- switch (whichHeap)
- {
- case noHeap:
- thePointer = 0L;
- theErr = memFullErr;
- break;
-
- case currentHeap:
- thePointer = NewPtrClear(byteCount);
- theErr = MemError();
- break;
-
- // System heap is growable into temp zone.
- case systemHeap:
- case tempHeap:
- thePointer = NewPtrSysClear(byteCount);
- theErr = MemError();
- break;
-
- // Error in ChooseHeap().
- default:
- thePointer = 0L;
- theErr = paramErr;
- break;
- }
-
- if (!theErr && !thePointer)
- theErr = memFullErr;
-
- if (theErr && thePointer)
- thePointer = 0L;
-
- if (resultErr)
- *resultErr = theErr;
-
- check((theErr == noErr) && (thePointer));
-
- return thePointer;
- }
-
- //--------------------------------------------------------------------- HappiGetResource
-
- Handle HappiGetResource (ResType theType, SInt16 theID, OSErr *resultErr)
- {
- Handle theResource;
- Handle resultingHandle;
- Size byteCount;
- SInt16 whichHeap;
- OSErr theErr;
-
- // We don't want to load the resource, just get its size.
- SetResLoad(false);
- theResource = GetResource(theType, theID);
- theErr = ResError();
- SetResLoad(true);
- require(theErr == noErr, bail);
-
- // Get resource size.
- byteCount = GetMaxResourceSize(theResource);
-
- // Determine which heap can fit the resource.
- whichHeap = ChooseHeap(byteCount, kMemReserve);
-
- switch (whichHeap)
- {
- case noHeap:
- resultingHandle = 0L;
- theErr = memFullErr;
- break;
-
- case currentHeap:
- resultingHandle = GetResource(theType, theID);
- require_action(resultingHandle, bail, theErr = ResError(););
- DetachResource(resultingHandle);
- theErr = ResError();
- break;
-
- // System heap is growable into temp zone.
- case systemHeap:
- case tempHeap:
-
- // Create a handle in temp mem.
- resultingHandle = TempNewHandle(byteCount, &theErr);
- require(resultingHandle, bail);
-
- // Load the resource data into it.
- HLockHi(resultingHandle);
- ReadPartialResource(theResource, 0L, (*resultingHandle), byteCount);
- theErr = ResError();
- HUnlock(resultingHandle);
- break;
-
- // Error in ChooseHeap().
- default:
- resultingHandle = 0L;
- theErr = paramErr;
- break;
- }
-
- bail:
-
- if (!theErr && !resultingHandle)
- theErr = memFullErr;
-
- if (theErr)
- resultingHandle = 0L;
-
- if (resultErr)
- *resultErr = theErr;
-
- check((theErr == noErr) && (resultingHandle));
-
- return resultingHandle;
- }
-
-
- #define MinFix(a,b) ( ((a)<(b))?(a):(b) )
- #define MaxFix(a,b) ( ((a)>(b))?(a):(b) )
- #define HighWord(x) ( (unsigned long)x>>16 )
-
-
- #pragma mark -------------------- Private Functions
- //===================================================================== Private Functions
- //--------------------------------------------------------------------- ChooseHeap
-
- static long ChooseHeap (Size bytesNeeded, Size reserve)
- {
- OSErr theErr;
- short whichHeap;
- Size currZoneTotalFree;
- Size sysZoneTotalFree;
- Size tempZoneTotalFree;
-
- theErr = GetZoneSizes(&currZoneTotalFree, &sysZoneTotalFree, &tempZoneTotalFree);
-
- if (theErr)
- whichHeap = noHeap;
- else
- {
- bytesNeeded += reserve;
-
- if (tempZoneTotalFree >= bytesNeeded)
- {
- // First, the Process Manager temp zone.
- whichHeap = tempHeap;
- }
- else if (currZoneTotalFree >= bytesNeeded)
- {
- // Second, the current zone.
- whichHeap = currentHeap;
- DebugStr("\pCurrent Heap");
- }
- else if (sysZoneTotalFree >= bytesNeeded)
- {
- // Third, the system zone (we assert because these are desparate times).
- whichHeap = systemHeap;
- DebugStr("\pSys Heap");
- }
- else
- {
- // If we can't leave reserve, select the biggest heap.
- // (we assert because these are desparate times)
- whichHeap = LargestHeap(currZoneTotalFree, sysZoneTotalFree, tempZoneTotalFree);
- DebugStr("\pLargest Heap");
- }
- }
-
- return whichHeap;
- }
-
- //--------------------------------------------------------------------- GetZoneSizes
-
- static OSErr GetZoneSizes (Size *currZoneTotalFree, Size *sysZoneTotalFree,
- Size *tempZoneTotalFree)
- {
- OSErr theErr;
- Boolean currZoneIsSysZone;
-
- // Init for error handling.
- theErr = noErr;
-
- // See if the current zone is the System Zone.
- currZoneIsSysZone = (GetZone() == SystemZone());
- theErr = MemError();
- check(theErr == noErr);
-
- if (!theErr)
- {
- // Free space in current zone.
- *currZoneTotalFree = MaxBlock();
- theErr = MemError();
- check(theErr == noErr);
- }
-
- if (!theErr)
- {
- // Free space in System zone.
- if (currZoneIsSysZone)
- {
- *sysZoneTotalFree = *currZoneTotalFree;
- }
- else
- {
- *sysZoneTotalFree = MaxBlockSys();
- theErr = MemError();
- check(theErr == noErr);
- }
- }
-
- if (!theErr)
- {
- // Free space in Process Manager temp zone.
- // !! this assumes that the _OSDispatch trap is available
- // !! i.e. that this code isnt being called at init-time.
- Size grow;
-
- *tempZoneTotalFree = TempMaxMem(&grow);
- theErr = MemError();
- check(theErr == noErr);
- }
-
- if (theErr)
- *currZoneTotalFree = *sysZoneTotalFree = *tempZoneTotalFree = 0;
-
- return theErr;
- }
-
- //--------------------------------------------------------------------- LargestHeap
-
- static long LargestHeap (Size currZoneTotalFree, Size sysZoneTotalFree,
- Size tempZoneTotalFree)
- {
- short whichHeap;
-
- if (currZoneTotalFree >= sysZoneTotalFree)
- {
- if (currZoneTotalFree >= tempZoneTotalFree)
- whichHeap = currentHeap;
- else
- whichHeap = tempHeap;
- }
- else
- {
- if (sysZoneTotalFree >= tempZoneTotalFree)
- whichHeap = systemHeap;
- else
- whichHeap = tempHeap;
- }
-
- return whichHeap;
- }
-
- //--------------------------------------------------------------------- SetMem
-
- static void SetMem (void *bytePtr, Size numBytes, UInt8 byteValue)
- {
- register SInt32 i;
- register SInt32 count;
- register UInt8 *ptr;
- register UInt8 value;
-
- count = numBytes;
- ptr = (UInt8 *)bytePtr;
- value = byteValue;
-
- for (i = 0; i < count; ++i)
- *ptr++ = value;
- }
-
-
-